# -*- coding: UTF8 -*-
# Author: Sakurazaki
# (Aitor Martin. @arienai_ne - sakurazaki.setsunita@gmail.com)

import pygame
from pygame.locals import *

import threading

class UpdateTh(threading.Thread):
    def __init__(self, l, delta):
        threading.Thread.__init__(self)
        self.l = l
        self.delta = delta

    def run(self):
        mouse = pygame.mouse.get_pos()
        for el in self.l:
            el.update(self.delta)
            if not el.hovering and el.rect.collidepoint(mouse): el.onHover()
            if el.hovering and not el.rect.collidepoint(mouse): el.onFade()
        

class GUI(object):

    def __init__(self):

        self.gEl = []
        self.hEl = []

    def handleEvent(self, ev):

        if ev.type == MOUSEBUTTONDOWN:
            for el in self.gEl:
                if el.rect.collidepoint(pygame.mouse.get_pos()): el.onClick()

    def update(self, delta):
        UpdateTh(self.gEl, delta).start()
        UpdateTh(self.hEl, delta).start()

    def render(self):
        for el in self.gEl: el.render()
        for el in self.hEl: el.render()

    def draw(self, surf):
        for el in self.gEl: surf.blit(el.surface, el.rect)
        for el in self.hEl: surf.blit(el.surface, el.rect)

    def addGuiElement(self, item):
        self.gEl.append(item)

    def addHudElement(self, item):
        self.hEl.append(item)

    def clear(self):
        self.gEl = []
        self.hEl = []
